package com.bigdata.demo.controller;

import com.bigdata.demo.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;

/**
 * Created by root on 3/1/18.
 */
@RefreshScope
@RestController
@RequestMapping("/api")
public class RedisController {

    private final Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Inject
    private RedisService redisService;

    @Value("${test.free}")
    private  String value;

    @RequestMapping(value = "/redis/value/{key}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> findValue(@PathVariable String  key) {
        logger.info("find by key:{}",key);

        Object value =redisService.getValue(key);
        return new ResponseEntity<>(value, HttpStatus.OK);

    }
    @RequestMapping(value = "/redis/list/{key}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> findList(@PathVariable String  key) {
        logger.info("find by key:{}",key);

        Object value =redisService.getList(key);
        return new ResponseEntity<>(value, HttpStatus.OK);

    }
    @RequestMapping(value = "/redis/set/{key}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> findset(@PathVariable String  key) {
        logger.info("find by key:{}",key);

        Object value =redisService.getSet(key);
        return new ResponseEntity<>(value, HttpStatus.OK);

    }
    @RequestMapping(value = "/redis/hash/{key}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> findHash(@PathVariable String  key) {
        logger.info("find by key:{}",key);
        System.out.println("dd:"+value);
        Object value =redisService.getHash(key);
        return new ResponseEntity<>(value, HttpStatus.OK);

    }

    @RequestMapping(value = "/redis/zset/{key}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> findZset(@PathVariable String  key) {
        logger.info("find by key:{}",key);

        Object value =redisService.getZset(key);
        return new ResponseEntity<>(value, HttpStatus.OK);

    }





}
